package com.example.calculate;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class CalculateActivity extends ActionBarActivity {

	TextView contentShow;
	
	String content = new String();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calculate);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		
		contentShow = (TextView)findViewById(R.id.content);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.calculate, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_calculate,
					container, false);
			return rootView;
		}
	}
	
	public void clickCalcuButtonHander(View view){
    	//System.out.println("a");
    	Button b = (Button)view;
		String name = (String)b.getText();
		//System.err.println(name);
		
		String lastStr = getLastStr();
		if(effect(name.charAt(0),lastStr!=null?lastStr.charAt(0):null)){
			String content = this.getContent()+name;
			this.setContent(content);
			showText();
		}
		
		
    }
	public boolean effect(Character inputStr,Character lastStr){
		
		if(lastStr==null){
			if(inputStr=='('||Calucate.isNumber(inputStr)){
				return true;
			}
		}else{
			if(Calucate.isNumber(inputStr)){
				if(Calucate.isNumber(lastStr)||Calucate.isSignCalu(lastStr)||lastStr=='('){
					return true;
				}
			}else{
				if(inputStr==')'){
					if(Calucate.isNumber(lastStr)||lastStr==')'){
						return true;
					}
				}else if(inputStr=='('){
					if(Calucate.isSignCalu(lastStr)||lastStr=='('){
						return true;
					}
				}else{
					if(Calucate.isNumber(lastStr)||lastStr==')'){
						return true;
					}
				}
				
			}
			
		}
		return false;
		
	}
	
	public boolean isNumberStr(char str){
		return Calucate.isNumber(str);
	}
	public String getLastStr(){
		int lenght = this.content.length();
		
		if(lenght==0){
			return null;
		}
		String lastStr = this.content.substring(lenght-1);
		return lastStr;
	}
    
    public void clickSpecialButtonHander(View view) {
    	//System.out.println("b");
    	
    	if(view.getId()==R.id.button_Backspace){
			String content = this.getContent().substring(0, this.getContent().length()-1);
			this.setContent(content);
		}else if(view.getId()==R.id.button_C){
			this.setContent(new String());
		}else if(view.getId()==R.id.button_calcute){
			String content = this.getContent();
			try {
				Double d = Calucate.recursiveCalStr(content);
				this.setContent(d.toString());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    	showText();
		
	}
    public void showText(){
    	contentShow.setText(content);  
    	contentShow.invalidate(); 
    }
	public TextView getContentShow() {
		return contentShow;
	}

	public void setContentShow(TextView contentShow) {
		this.contentShow = contentShow;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

}
